package com.tb.dubbo_service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tb.dubbo.IOrderService;
import com.tb.mapper.OrderMapper;
import com.tb.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override//所有订单
    public Map<String, Object> getAllOrder(Map<String, Object> map) {
        long count = orderMapper.getAllOrder(map).size();
        System.out.println(count);
        PageHelper.startPage((Integer) map.get("page"), (Integer) map.get("rows"));
        List<Order> list = orderMapper.getAllOrder(map);
        Map<String, Object> rmap = new HashMap<>();
        rmap.put("count", count);
        rmap.put("olist", list);
        return rmap;
    }

    @Override
    public Map<String, Object> getOrderByStatusId(Map<String, Object> map) {
        List<Order> list = orderMapper.getOrderByStatusId(map);
        if(list==null||list.size()==0){
            return null;
        }
        long count = list.size();
        PageHelper.startPage((Integer) map.get("page"), (Integer) map.get("rows"));
        List<Order> olist = orderMapper.getOrderByStatusId(map);
        Map<String, Object> rmap = new HashMap<>();
        rmap.put("count", count);
        rmap.put("olist", olist);
        return rmap;
    }

    @Override
    public Map<String, Object> getOrderByOid(Map<String, Object> map) {
        Map<String, Object> rmap = new HashMap<>();
        //最大行数
        Order or = orderMapper.getOrderByOid(map);
        Long count = null;
        if (or != null) {
            count = (long) or.getOrderItem().size();
        } else {
            rmap.put("count", null);
            rmap.put("order", null);
        }
        PageHelper.startPage((Integer) map.get("page"), (Integer) map.get("rows"));
        //分页后详情
        Order order = orderMapper.getOrderByOid(map);

        rmap.put("count", count);
        rmap.put("order", order);
        return rmap;
    }

    //获取所有用户订单
    @Override
    public Map<String, Object> getAllUserOrder(Map<String, Integer> map) {
        long count = orderMapper.selectAllUser().size();
        PageHelper.startPage(map.get("page"), map.get("rows"));
        List<Order> olist = orderMapper.selectAllUser();
        Map<String, Object> rmap = new HashMap<>();
        rmap.put("count", count);
        rmap.put("olist", olist);
        return rmap;
    }

    @Override
    public int updateAddress(Order order) {
        int flag = orderMapper.updateAddress(order);
        return flag;
    }

    @Override
    public int retrunId() {
        int flag = orderMapper.retrunId();
        return flag;
    }

    @Override
    public int updateOrderStatus(Order order) {
        int i = orderMapper.updateOrderStatus(order);
        return i;
    }

    @Override
    public int insertOrders(Order order) {
        return orderMapper.insertOrders(order);
    }

}

